/**
 * 流程视图
 */
//模型文件版本数据
var processVersioninfoStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showProcessVersionInfo.action?method=showProcessVersionInfo',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'versionInfo',
		totalProperty : 'totalVersionInfoSize',
		id : 'processversion'
	}, [ {
		name : 'groupId'
	}, {
		name : 'time'
	},{
		name : 'author'
	}, {
		name : 'versionId'
	}
	])
});
//版本表格列模型
var processVersioninfosm=new Ext.grid.CheckboxSelectionModel ({singleSelect : false});
var versioninfoColumnModel=new Ext.grid.ColumnModel(
		[processVersioninfosm,
		 new Ext.grid.RowNumberer(),
		 {
			header : '版本号',
			width : 100,
			dataIndex : 'versionId',
			sortable : true,
			align : 'center'
		 },   {
			 header : '修改日期',
			 width : 200,
			 dataIndex : 'time',
			 sortable : true,
             align : 'center'
		 },{
			 header : '作者',
			 width : 100,
			 dataIndex : 'author',
			 sortable : true,
			 align : 'center'
         },{
			 header : '模型种类',
			 width : 100,
			 dataIndex : 'groupId',
			 sortable : true,
			 align : 'center'
         }]
          );

//模型版本信息列表
var versioninfoList=new Ext.grid.EditorGridPanel({
	id:'processversion',
	title: "版本历史",
	region:'center',
	ds : processVersioninfoStore ,	//绑定store
	enableColumnResize : true,
	border : true,
	//clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : processVersioninfosm,
    bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: processVersioninfoStore ,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
	cm : versioninfoColumnModel,
	store : processVersioninfoStore 
});
var versioninfoTabPanel = new Ext.TabPanel({
	border : true,
	region:'south',
	autoScroll:true,
	//clicksToEdit:1,
	//autoHeight:true,
    //autoWidth:true,
	height:200,
	items:[ versioninfoList,
	{
	title: "功能具体信息",
	region:'center',
	
	collapsible : true,
	border:false,
	contentEl:'functionDetail'
	}]
	,
	activeItem: 1
	});
var process = new Ext.Panel({
	id : 'process-panel',
	title : '流程视图',
	layout : 'border',
	//closable:true,
	defaults : {
		
		bodyStyle : 'padding:15px'
	},
	items : [  versioninfoTabPanel,
	           {
		region : 'center',
		border : false,
		autoScroll:true,
		contentEl : 'processCanvas'
	} ]
});

function returnToProcessList(){
	
	  Ext.getCmp('content-panel').layout.setActiveItem('process-list-panel');
	  showProcessList();
}
var processid;
var pText;

var processVersionStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showProcessVersionInfo.action?method=showProcessVersionInfo',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "ProcessVersion",
		totalProperty : "totalProcessVersion",
		id:"groupId"
    }, [ {
		name : 'groupId'
	}])
}); 


var functionStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showFunction.action?method=showFunction',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "function",
		totalProperty : "totalFunction",
		id:"id"
    }, [ {
		name : 'id'
	}, {
		name : 'fileId'
	}, {
		name : 'enName'
	},{
		name : 'cnName'
	},{
		name : 'description'
	}])
});
function showProcessContent(cid,text) {
	
    processid=cid;
    processVersioninfoStore.load({
    	params:{
    		cid:cid
    	},
    	callback: function(r, options, success) { 
    		if (success == false) { 
    		alert("加载process-version数据失败，无对应数据或者系统出现异常！"); 
    		}
    		else{
    		//alert("加载domain-version数据成功！");	
    		}
    	}
    });
    pText=text;
    functionStore.load({ 
    	params : {
			cid : cid
		},
		callback: function(r, options, success) { 
		if (success == false) {  
		alert("加载数据失败，无对应数据或者系统出现异常！"); 
		}else{ 
			var canvas=document.getElementById('processCanvas');
			var ctx=canvas.getContext('2d');
			 drawProcessModel(300,200,ctx,functionStore,text,canvas);
			 Ext.getDom('functionDetail').innerHTML = "";
			 var cm=new Ext.grid.ColumnModel([
			              					new Ext.grid.RowNumberer(),
			              					{header:'编号',dataIndex:'id'},
			              					{header:'名称(中文)',dataIndex:'cnName'},
			              					{header:'名称(英文)',dataIndex:'enName'},
			              					{header:'备注',dataIndex:'description'},
			              					{header:'文件Id',dataIndex:'fileId'}
			              				]);
			              			var grid=new Ext.grid.GridPanel({
			              					renderTo:'functionDetail',
			              					 viewConfig: {
			              			            forceFit:true
			              			        },
			              					layout:'fit',
			              					stripeRows: true,
			              					autoHeight:true,
			              					loadMask:true,
			              					store: functionStore,
			              					cm:cm
			              					,			              						
			              					listeners : {
			              						cellclick : {
			              						fn : function(grid, rowIndex, columnIndex, e) {
			              							//点击记录，在表单中显示记录信息
			              							selected_row = rowIndex;
			              						var record = grid.getStore().getAt(rowIndex);
			              						//var fAmount=grid.getCount();			              									          			              				       		              				    
			              						Ext.getCmp('content-panel').addTab('functionpoint-panel');
			              				      	 showFunctionContent(functionStore.getAt(rowIndex).get('id'),functionStore.getAt(rowIndex).get('cnName'),processid,pText);
			              				       
			              				      
			              				   
			              						},
			              						scope : this
			              					}
			              				}
			              				});
			}
		}
		});
   }
function drawProcessModel(startx,starty,ctx,functionStore,text,canvas){
	 ctx.restore();
	 ctx.clearRect(0, 0, 800, 800);
	 ctx.save();
	 ctx.translate(startx, starty);
	 var fAmount=functionStore.getCount();
	
	 var gradient = ctx.createLinearGradient(0, 0, 0, 50);
	 gradient.addColorStop(0, 'rgb(154, 205, 50)');
	 gradient.addColorStop(1, 'rgb(255,255,240)');
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 100, 50); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.lineWidth = 1;
	 ctx.rect(0.5,0.5, 100, 50);
	 ctx.stroke();              //黑边框
	 
	 var processImage=new Image();
	 processImage.onload=function(){
	 ctx.drawImage(processImage,10,12,16,16);
	 };
	 processImage.src='images/modelUnit/function/process.gif';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "10pt 宋体";
	 ctx.fillText(text,30,25,70); //标题栏
	 
	
	 var functionImg=new Image();
	 functionImg.onload=function(){
     for (var i=0;i<fAmount;i++){
    	    x=parseInt(-150*Math.cos(i*Math.PI*2/fAmount));
    	    if (x>100) x=1.2*x;
    	    y=parseInt(-150*Math.sin(i*Math.PI*2/fAmount));
    	     gradient = ctx.createLinearGradient(x, y, 0, 50);
    		 gradient.addColorStop(0, 'rgb(230, 230, 250)');
    		 gradient.addColorStop(1, 'rgb(255,255,240)');
    		 ctx.fillStyle = gradient;
    		 ctx.fillRect( x, y, 100, 50); //渐变的背景色
    		
    		 ctx.lineWidth = 1;
    		 ctx.strokeStyle = '#000000'; 
    		 ctx.beginPath(); 
    		
    		 ctx.rect(x-0.5,y-0.5, 100, 50);
    		 ctx.stroke();              //黑边框
    		 
			ctx.drawImage(functionImg,x+10,12+y,16,16);
			var record=functionStore.getAt(i);	
			ctx.fillStyle ="black";
			ctx.font = "10pt Arial ";
			ctx.fillText(record.get('cnName'),x+30,y+25,70);
			ctx.fillText('['+record.get('enName')+']',x,y+45,100);
			drawLine(x+50,y+25,50,25,ctx,100,50);
		 }
        canvas.onclick = onMouseClick;     
	 };
	 functionImg.src='images/modelUnit/function/function_point.gif';
	 }
function onMouseClick(event) {
	 var fAmount=functionStore.getCount();
	 var i=0;
	 var x=event.layerX-300;
	 var y=event.layerY-200;
	 if (x/1.2>100) x=x/1.2;
	 while (i<fAmount){
     
     if (x>-150*Math.cos(i*Math.PI*2/fAmount)
    	 && x<-150*Math.cos(i*Math.PI*2/fAmount)+100
         && y>-150*Math.sin(i*Math.PI*2/fAmount)
         && y<-150*Math.sin(i*Math.PI*2/fAmount)+50)
    {
    	
//    	 authenticate(functionStore.getAt(i).get('fileId'),'read',function(){
   	       
   	       showFunctionContent(functionStore.getAt(i).get('id'),functionStore.getAt(i).get('cnName'),processid,pText);
   	       Ext.getCmp('content-panel').addTab('functionpoint-panel');
//      	 });
      	 break;
     }
      i++;
	 }
}  


